/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

FIVe3D
Flash Interactive Vector-based 3D
Mathieu Badimon  |  five3d@mathieu-badimon.com

http://five3D.mathieu-badimon.com  |  http://five3d.mathieu-badimon.com/archives/  |  http://code.google.com/p/five3d/

/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import net.badimon.five3D.display.Sprite;

class net.badimon.five3D.utils.Drawing {

	public static function polygon(sprite:Sprite, points:Array, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		tracePolygon(sprite, points);
		sprite.endFill();
	}

	private static function tracePolygon(sprite:Sprite, points:Array):Void {
		var i:Number = points.length;
		with (sprite) {
			moveTo(points[0].x, points[0].y);
			while(i--) lineTo(points[i].x, points[i].y);
		}
	}

	public static function rectangle(sprite:Sprite, width:Number, height:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceRectangle(sprite, 0, 0, width, height);
		sprite.endFill();
	}

	public static function rectanglePlace(sprite:Sprite, x:Number, y:Number, width:Number, height:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceRectangle(sprite, x, y, x + width, y + height);
		sprite.endFill();
	}

	private static function traceRectangle(sprite:Sprite, x1:Number, y1:Number, x2:Number, y2:Number):Void {
		with (sprite) {
			moveTo(x1, y1);
			lineTo(x2, y1);
			lineTo(x2, y2);
			lineTo(x1, y2);
			lineTo(x1, y1);
		}
	}

	public static function disk(sprite:Sprite, radius:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceDisk(sprite, 0, 0, radius);
		sprite.endFill();
	}

	public static function diskPlace(sprite:Sprite, x:Number, y:Number, radius:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceDisk(sprite, x, y, radius);
		sprite.endFill();
	}

	private static function traceDisk(sprite:Sprite, x:Number, y:Number, radius:Number):Void {
		var A:Number = (Math.SQRT2 - 1) * radius;
		var B:Number = radius / Math.SQRT2;
		with (sprite) {
			moveTo(x, y - radius);
			curveTo(x + A, y - radius, x + B, y - B);
			curveTo(x + radius, y - A, x + radius, y);
			curveTo(x + radius, y + A, x + B, y + B);
			curveTo(x + A, y + radius, x, y + radius);
			curveTo(x - A, y + radius, x - B, y + B);
			curveTo(x - radius, y + A, x - radius, y);
			curveTo(x - radius, y - A, x - B, y - B);
			curveTo(x - A, y - radius, x, y - radius);
		}
	}

	public static function rounded(sprite:Sprite, width:Number, height:Number, radius:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceRounded(sprite, 0, 0, width, height, radius);
		sprite.endFill();
	}

	public static function roundedPlace(sprite:Sprite, x:Number, y:Number, width:Number, height:Number, radius:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceRounded(sprite, x, y, x + width, y + height, radius);
		sprite.endFill();
	}

	private static function traceRounded(sprite:Sprite, x1:Number, y1:Number, x2:Number, y2:Number, radius:Number):Void {
		var A:Number = (Math.SQRT2 - 1) * radius;
		var B:Number = radius / Math.SQRT2;
		with (sprite) {
			moveTo(x1 + radius, y1);
			lineTo(x2 - radius, y1);
			curveTo(x2 - radius + A, y1, x2 - radius + B, y1 + radius - B);
			curveTo(x2, y1 + radius - A, x2, y1 + radius);
			lineTo(x2, y2 - radius);
			curveTo(x2, y2 - radius + A, x2 - radius + B, y2 - radius + B);
			curveTo(x2 - radius + A, y2, x2 - radius, y2);
			lineTo(x1 + radius, y2);
			curveTo(x1 + radius - A, y2, x1 + radius - B, y2 - radius + B);
			curveTo(x1, y2 - radius + A, x1, y2 - radius);
			lineTo(x1, y1 + radius);
			curveTo(x1, y1 + radius - A, x1 + radius - B, y1 + radius - B);
			curveTo(x1 + radius - A, y1, x1 + radius, y1);
		}
	}

	public static function star(sprite:Sprite, nbtops:Number, radius1:Number, radius2:Number, angle:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceStar(sprite, 0, 0, nbtops, radius1, radius2, angle);
		sprite.endFill();
	}

	public static function starPlace(sprite:Sprite, x:Number, y:Number, nbtops:Number, radius1:Number, radius2:Number, angle:Number, color:Number, alpha:Number):Void {
		sprite.beginFill(color, alpha);
		traceStar(sprite, x, y, nbtops, radius1, radius2, angle);
		sprite.endFill();
	}

	private static function traceStar(sprite:Sprite, x:Number, y:Number, nbtops:Number, radius1:Number, radius2:Number, angle:Number):Void {
		nbtops *= 2;
		var anglestep:Number = Math.PI * 2 / nbtops;
		with (sprite) {
			moveTo(x + Math.cos(angle) * radius2, y + Math.sin(angle) * radius2);
			while (nbtops--) {
				angle += anglestep;
				if (nbtops % 2) lineTo(x + Math.cos(angle) * radius1, y + Math.sin(angle) * radius1);
				else lineTo(x + Math.cos(angle) * radius2, y + Math.sin(angle) * radius2);
			}
		}
	}

	public static function gradientHorizontalPlain(sprite:Sprite, width:Number, height:Number, color:Number, alpha1:Number, alpha2:Number, nbsteps:Number):Void {
		var widthstep:Number = width / nbsteps;
		var alphastep:Number = (alpha2 - alpha1) / nbsteps;
		for (var i:Number = 0;i < nbsteps; i++) rectanglePlace(sprite, i * widthstep, 0, widthstep, height, color, alpha1 + i * alphastep);
	}

	public static function gradientHorizontalPlainPlace(sprite:Sprite, x:Number, y:Number, width:Number, height:Number, color:Number, alpha1:Number, alpha2:Number, nbsteps:Number):Void {
		var widthstep:Number = width / nbsteps;
		var alphastep:Number = (alpha2 - alpha1) / nbsteps;
		for (var i:Number = 0;i < nbsteps; i++) rectanglePlace(sprite, x + i * widthstep, y, widthstep, height, color, alpha1 + i * alphastep);
	}
}